/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.treeQuery;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;

/**
 *
 * @author jorgeorm
 */
public final class CheckBoxNode extends DefaultMutableTreeNode {

  protected boolean isSelected;
  protected boolean hasGroup;
//  Ultimo intento
//  
//  public JCheckBox box;
//  }

//  public CheckBoxNode(CheckBoxNode nodo)
//  {
//      this(nodo.getUserObject(), nodo.getAllowsChildren(), nodo.isSelected());
//      this.isSelected= nodo.isSelected();
//      this.hasGroup= nodo.hasGroup();
//  }
  
  public CheckBoxNode() {
    this(null);
    isSelected=false;
    hasGroup=false;
//    box=new JCheckBox();
  }

  public CheckBoxNode(Object userObject) {
    this(userObject, true, false);
  }

  public CheckBoxNode(Object userObject, boolean allowsChildren,
      boolean isSelected) {
    super(userObject, allowsChildren);
    this.isSelected = isSelected;
  }
  
    public void add(CheckBoxNode newChild) {
        super.add(newChild);
        newChild.setParent(this);
    }
  public void setSelected(boolean isSelected) {
    this.isSelected = isSelected;
  }
  
  public void setHasGroup(boolean hasGroup)
  {
      this.hasGroup=hasGroup;
  }
  
  public boolean hasGroup()
  {
      return this.hasGroup;
  }

  public boolean isSelected() {
    return isSelected;
  }
  
  @Override
  public CheckBoxNode clone()
  {
      CheckBoxNode tmp= new CheckBoxNode();
      tmp.setSelected(this.isSelected);
      tmp.setUserObject(this.userObject.toString());
      tmp.setAllowsChildren(this.allowsChildren);
      tmp.setHasGroup(this.hasGroup);
      if(this.children!=null)
      {
          tmp.children=new Vector();
//          Copia profunda de los hijos
//          
          for(int i=0; i<this.children.size();i++)
          {
              tmp.children.add(((CheckBoxNode)this.children.get(i)).clone());
          }
      }
      
      tmp.setParent((MutableTreeNode)this.getParent());
      return tmp;
  }
  
  public boolean esAncestro(CheckBoxNode presunto)
  {
      boolean out=false;
      CheckBoxNode padre=(CheckBoxNode)this.getParent();
      String lineaAsc="";
      while(padre!=null)
      {
          lineaAsc=padre.toString()+"<-"+lineaAsc;
          if(presunto.equals(padre))
          {
              out=true;
              break;
          }else{
              padre=(CheckBoxNode)padre.getParent();
          }
      }
      System.out.println("Linaje: "+lineaAsc);
      return out;
  }

//  public void setSelectionToSons(boolean sel)
//    {
//        
//        ArrayList<CheckBoxNode>descendencia=new ArrayList<CheckBoxNode>();
//        descendencia.add(this);
//        CheckBoxNode tmp=null;
//        while(!(descendencia.isEmpty()))
//        {
//            tmp=descendencia.remove(0);
//            tmp.setSelected(sel);
//            System.out.println("En: "+tmp+", Con: "+tmp.isSelected());
//            if(!(tmp.isLeaf())){
//                ArrayList<CheckBoxNode>hijos=new ArrayList<CheckBoxNode>();
//                for(int i=0;i<tmp.children.size();i++){
//                    hijos.add((CheckBoxNode)tmp.children.get(i));
//                }
//                hijos.addAll(descendencia);
//                descendencia=hijos;
//            }
//        }
//    }
  
  // If you want to change "isSelected" by CellEditor,
  /*
   public void setUserObject(Object obj) { if (obj instanceof Boolean) {
   * setSelected(((Boolean)obj).booleanValue()); } else {
   * super.setUserObject(obj); } }
   */

}
